package com.concurrent.data.thread.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 哲思小辉
 * @version 1.0.0
 * @ClassName JackSonJsonUtil.java
 * @Description Json工具类
 * @Param
 * @createTime 2020年03月25日 16:58:00
 */
@Slf4j
public class JackSonJsonUtil {
    private JackSonJsonUtil() {
    }

    private static final ObjectMapper objectMapper = new ObjectMapper();

    static{
        //序列化的时候序列对象的所有属性
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);

        //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));


        // 对于空的对象转json的时候不抛出错误
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

        // 禁用遇到未知属性抛出异常
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);

        objectMapper.enable(JsonParser.Feature.ALLOW_COMMENTS);
        objectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
        objectMapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);

    }

    /**
     * 转成json 字符串 （不保留格式，换行、空格都会去掉）
     *
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        String jsonStr = null;
        try {
            // 针对的是转义字符串
            if (object instanceof String) {
                JsonNode jsonNode = objectMapper.readTree(String.valueOf(object));
                jsonStr = objectMapper.writeValueAsString(jsonNode);
            } else {
                jsonStr = objectMapper.writeValueAsString(object);
            }
        } catch (JsonProcessingException e) {
            log.error("json数据格式化报异常{}", e);
        }

        return jsonStr;
    }


    /**
     * 转成Map<String,Object>
     *
     * @param object
     * @return
     */
    public static Map<String, Object> toMap(Object object) {
        String text = toJson(object);
        Map<String, Object> map = null;
        if (text != null) {
            try {
                map = objectMapper.readValue(text, Map.class);
            } catch (JsonProcessingException e) {
                log.error("json字符串转Map异常：{}", e);
            }
        }
        return map;
    }

    /**
     * 转成Map<String,T>
     *
     * @param object
     * @return
     */
    public static <T> Map<String, T> toMap(Object object, Class<T> clzz) {
        String text = toJson(object);
        Map<String, T> map = null;
        if (text != null && clzz != null) {
            try {
                JavaType javaType = objectMapper.getTypeFactory().constructMapType(Map.class, String.class, clzz);
                map = objectMapper.readValue(text, javaType);
            } catch (JsonProcessingException e) {
                log.error("json字符串转Map异常：{}", e);
            }
        }
        return map;
    }

    /**
     * 转成List <String>
     *
     * @param object
     * @return
     */
    public static List toList(Object object) {
        String text = toJson(object);
        List list = null;
        if (text != null ) {
            try {
                list = objectMapper.readValue(text, List.class);
            } catch (JsonProcessingException e) {
                log.error("json字符串转List异常：{}", e);
            }
        }
        return list;
    }

    /**
     * 转成List<T>
     *
     * @param object
     * @param clzz
     * @param <T>
     * @return
     */
    public static <T> List<T> toList(Object object, Class<T> clzz) {
        String text = toJson(object);
        List<T> list = null;
        if (text != null) {
            try {
                JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, clzz);
                list = objectMapper.readValue(text, javaType);
            } catch (JsonProcessingException e) {
                log.error("json字符串转List<T>异常：{}", e);
            }
        }
        return list;
    }

    /**
     * 转成JavaBean
     *
     * @param object
     * @param clzz
     * @param <T>
     * @return
     */
    public static <T> T toJavaBean(Object object, Class<T> clzz) {
        String text = toJson(object);
        T t = null;
        if (text != null) {
            try {
                t = objectMapper.readValue(text, clzz);
            } catch (JsonProcessingException e) {
                log.error("json字符串转Java实体异常：{}", e);
            }
        }
        return t;
    }

    /**
     * 转成Set
     *
     * @param object
     * @return
     */
    public static Set toSet(Object object) {
        String text = toJson(object);
        Set set = null;
        if (text != null) {
            try {
                set = objectMapper.readValue(text, Set.class);
            } catch (JsonProcessingException e) {
                log.error("json字符串转Set异常：{}", e);
            }
        }
        return set;
    }

    /**
     * 转成Set<T>
     *
     * @param object
     * @param clzz
     * @return
     */
    public static <T> Set<T> toSet(Object object, Class<T> clzz) {
        String text = toJson(object);
        Set<T> set = null;
        if (text != null) {
            try {
                JavaType javaType = objectMapper.getTypeFactory().constructCollectionLikeType(Set.class, clzz);
                set = objectMapper.readValue(text, javaType);
            } catch (JsonProcessingException e) {
                log.error("json字符串转Set<T>异常：{}", e);
            }
        }
        return set;
    }

}


